﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace JLM2
{
    public static class JLMUtlity
    {

        /// <summary>
        /// lrc算法函数
        /// </summary>
        /// <param name="data">byte数组</param>
        /// <returns>计算出的lrc值</returns>
        static public byte CalcLRC(byte[] data)
        {
            byte lrc = 0;
            for (int i = 0; i < data.Length; i++)
            {
                lrc += data[i];
            }
            lrc %= 255;
            lrc = (byte)~lrc;
            lrc += 1;
            return lrc;
        }

        /// <summary>
        /// 将16进制字符串转换为16进制数组
        /// </summary>
        /// <param name="hexString">16进制字符串</param>
        /// <returns></returns>
        static public byte[] HexStringToHexBuffer(string hexString)
        {
            //string testCmd2 = "01 03 06 14 00 08";
            string[] chars = hexString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            byte[] returnBytes = new byte[chars.Length];
            for (int i = 0; i < chars.Length; i++)
            {
                returnBytes[i] = Convert.ToByte(chars[i], 16);
            }
            return returnBytes;
        }

        /// <summary>
        /// 将获取的2位16进制共计8位字符串，转成32位十进制结果。
        /// </summary>
        /// <param name="HexString"></param>
        /// <returns></returns>
        public static double Get32BitResult(string HexString)
        {
            if (HexString.Length != 8) return 0;
            var firstValue = int.Parse(HexString.Substring(0, 4), System.Globalization.NumberStyles.HexNumber);
            var secondValue = int.Parse(HexString.Substring(4, 4), System.Globalization.NumberStyles.HexNumber);
            return firstValue + secondValue * 65536;
        }

        /// <summary>
        /// 将32位十进制数值转成2位16进制8位字符串，分拆成数组
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static List<string> Change32BitToHesString(int Value)
        {
            if (Value > 65536)
            {
                int First = Value % 65536;
                int Second = Convert.ToInt32(Math.Floor(Convert.ToDecimal(Value / 65536)));
                Utils.WriteLog("JLMUtility", $"First={First} Second={Second}");
                var result = new string[] { Convert.ToString(First, 16).PadLeft(4, '0').Insert(2, " "), Convert.ToString(Second, 16).PadLeft(4, '0').Insert(2, " ") }.ToList();
                Utils.WriteLog("JLMUtility", $"result0={result[0]} result1={result[1]}");
                return result;
            }
            else
            {
                var result = new string[] { Convert.ToString((int)(Value), 16).PadLeft(4, '0').Insert(2, " "), Convert.ToString(0, 16).PadLeft(4, '0').Insert(2, " ") }.ToList();
                Utils.WriteLog("JLMUtility", result[0] + " " + result[1]);
                return result;
            }
        }
    }
}